package com.juzipi.demo.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

public class JdbcInsertDemo {
    public static void main(String[] args) {
        //练习
        Statement statement = null;
        Connection connection = null;
        try {
            //可以省略注册
            Class.forName("com.mysql.cj.jdbc.Driver");
            //定义sql
            String s = "insert into practice VALUES (null,'张舞',20,'男')";
            //获取 Connection 对象
            connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/springboot?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone = GMT", "zhangsan", "lsjxj");
            //获取执行sql的对象 Statement
            statement = connection.createStatement();
            //执行sql
            int executeUpdate = statement.executeUpdate(s);
            System.out.println(executeUpdate);
            if (executeUpdate>0){
                System.out.println("SUCCESS");
            }else {
                System.out.println("ERROR");
            }

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        } finally {
            if (statement != null){
                //不为空再释放资源，避免空指针异常
                try {
                    statement.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }

            if (connection != null){
                //不为空再释放资源，避免空指针异常
                try {
                    connection.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }

            }
        }




    }
}
